import warnings
warnings.warn("qturboframe.public_ui.ucf_h_ui.base_table模块已被废弃，替代模块为qturboframe.public_ui.h_ui.base_table", DeprecationWarning)

from qturboframe.lib.ranorex.exceptions import CsClientError
from qturboframe.lib.ranorex.web_element.table_tag import TableTag
from qturboframe.lib.ranorex.web_element.tr_tag import TrTag
from qturboframe.lib.ranorex.web_element.th_tag import ThTag
from qturboframe.lib.ranorex.web_element.td_tag import TdTag
from qturboframe.lib.ranorex.web_element.span_tag import SpanTag
from ..ucf_base_element import BaseElement


class BaseTable(BaseElement):

    def __init__(self, element_para, father=None, timeout=10000):
        super().__init__(element_para, father, timeout)
        self._element = TableTag(self._element.control)

    def get_title_list(self):
        """
        获取hui表格的列名，hidden列除外，以列表的形式返回

        :Returns:
            返回表格列名集合

        :Raises:
            SeleniumError: 参数为非table元素
        """
        table_hidden_class = r'h-table-hidden'
        grid_hidden_class = r'h-editgird-hidden'
        gutter_class = r'gutter'
        if self._element.tag_name == 'table':
            title_list = list()
            tr = TrTag(self._element.find_elements_by_attribute(
                'xpath', './thead/tr', timeout=self.timeout)[-1].control)
            th_list = [ThTag(webelement.control) for webelement in
                       tr.find_elements_by_attribute('xpath', './th', timeout=self.timeout)]
            for th in th_list:
                th_class = th.get_attribute('class').split(' ')
                if (table_hidden_class not in th_class and grid_hidden_class not in th_class and
                        gutter_class not in th_class):
                    th.focus()
                    text = SpanTag(th.find_element_by_attribute(
                        'xpath', './div[@class~\'h-table-cell\']/span[@class~\'span-cell\']').control).text
                    title_list.append(text)
            return title_list
        else:
            raise CsClientError('元素对象不为table对象，对象类型为： ' +self._element.tag_name)

    def get_tr_list(self):
        """
        获取hui表格的列名，以列表的形式返回

        :Returns:
            返回表格行集合

        :Raises:
            SeleniumError: 参数为非table元素
        """
        if self._element.tag_name == 'table':
            tr_list = [TdTag(webelement.control) for webelement in
                       self._element.find_elements_by_attribute('xpath', './tbody/tr', timeout=self.timeout)]
            return tr_list
        else:
            raise CsClientError('元素对象不为table对象，对象类型为：' + self._element.tag_name)

    def get_text_list(self, columns=0, column=0):
        '''
        获取hui表格列文本集合

        :Args:
         - columns: 表格总列数
         - column: 所需获取列的列号

        :Returns:
            返回当前页的列文本集合

        :Raises:
            SeleniumError: 参数为非table元素
        '''
        if self._element.tag_name == 'table':
            body_list = list()
            td_list = self.get_cell_list()
            for i in range(0 ,len(td_list)):
                if columns:
                    if (i + 1) % columns == column % columns:
                        text = td_list[i].find_element_by_attribute(
                            'xpath', './div[@class~\'h-table-cell\']/*').text
                        body_list.append(text)
                else:
                    text = td_list[i].find_element_by_attribute(
                            'xpath', './div[@class~\'h-table-cell\']/*').text
                    body_list.append(text)
            return body_list
        else:
            raise CsClientError('元素对象不为table对象，对象类型为：' + self._element.tag_name)

    def get_cell_list(self, columns=0, column=0):
        """
        获取hui表格列单元格集合

        :Returns:
            返回当前页的列单元格集合

        :Raises:
            SeleniumError: 参数为非table元素
        """
        table_hidden_class = r'h-table-hidden'
        grid_hidden_class = r'h-editgird-hidden'
        if self._element.tag_name == 'table':
            body_list = list()
            td_list = [TdTag(webelement.control) for webelement in
                       self._element.find_elements_by_attribute('xpath', './tbody/tr/td', timeout=self.timeout)]
            for td in td_list:
                td_class = td.get_attribute('class').split(' ')
                if table_hidden_class not in td_class and grid_hidden_class not in td_class:
                    body_list.append(td)
            cell_list = list()
            for i in range(0, len(body_list)):
                if columns:
                    if (i + 1) % columns == column % columns:
                        cell_list.append(body_list[i])
                else:
                    cell_list.append(body_list[i])
            return cell_list
        else:
            raise CsClientError('元素对象不为table对象，对象类型为：' + self._element.tag_name)

    def get_cell_list_in_row(self, row):
        '''

        :param row:
        :return:
        '''
        table_hidden_class = r'h-table-hidden'
        grid_hidden_class = r'h-editgird-hidden'
        if self._element.tag_name == 'table':
            cell_list = list()
            tr = self.get_tr_list()[row - 1]
            tr.focus()
            td_list = [TrTag(webelement.control) for webelement in
                       tr.find_elements_by_attribute('xpath', './td', timeout=self.timeout)]
            for td in td_list:
                td_class = td.get_attribute('class').split(' ')
                if table_hidden_class not in td_class and grid_hidden_class not in td_class:
                    cell_list.append(td)
            return cell_list
        else:
            raise CsClientError('元素对象不为table对象，对象类型为：' + self._element.tag_name)
